//
//  Plugin_DeviceInfo.m
//  Unity-iPhone
//
//  Created by 涂俊 on 2018/3/29.
//  IOS设备具体参数：http://www.blakespot.com/ios_device_specifications_grid.html
//

#import "Plugin_DeviceInfo.h"
#import "SSDKDataKeys.h"
#import "SSDKEventKeys.h"
#import "SSDKEventManager.h"
#import "SuperSDKPluginTools/SSDKPluginJsonUtil.h"
#import "SuperSDKPluginTools/SSDKPluginDeviceUtil.h"
#import <AdSupport/AdSupport.h>
#import "sys/utsname.h"
#include <ifaddrs.h>
#include <arpa/inet.h>


@implementation Plugin_DeviceInfo

{
    NSString *callBackObj;
    NSString *callBackFun;
}

- (void) initInfo {
    [SSDKEventManager addObsver:self selector:@selector(getInfoAsync:) name:@"plugin_event_deviceinfo" object:nil className:@"Plugin_DeviceInfo"];
}

//异步返回设备信息
- (void) getInfoAsync:(NSNotification*) notification {
    callBackObj=[[notification object] objectForKey:@"UNITY_OBJ"];
    callBackFun=[[notification object] objectForKey:@"UNITY_PLUGIN_CALLBACK"];
    NSString *key = [[notification object] objectForKey:@"deviceKey"];
    
    NSString *result = [[self class] getDeviceInfo:key];
    NSLog(@"获取设备信息.key=%@,rusult=%@",key,result);
    if (callBackObj!=nil && callBackFun!=nil) {
        [SSDKEventManager postNotificationName:EVENT_PLUGIN_CALLBACK
                                        object:@{CALLBACK_E_OBJ: callBackObj,
                                                 CALLBACK_E_METHOD: callBackFun,
                                                 CALLBACK_E_MSG: result}];
    }
}

//同步返回设备信息
+(NSString *) getDeviceInfo:(NSString *)key {
    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init];
    if([key isEqualToString:@"all"]) {
        NSArray *keys = @[@"deviceId", @"os", @"osVersion", @"appVersion", @"deviceType", @"deviceModel", @"versionCode", @"resolution", @"CPUCores", @"curCPUFreq", @"maxCPUFreq", @"minCPUFreq", @"GPUModel", @"GPUShader", @"GPUVersion"];
        for (NSString *k in keys) {
            [jsonDic setValue:[[self class] getSingleInfo:k] forKey:k];
        }
    } else {
        NSString *value = [[self class] getSingleInfo:key];
        if (value != nil) {
            [jsonDic setValue:value forKey:key];
        }
    }
    return [SSDKPluginJsonUtil toJsonWithObject:jsonDic];
}

+(NSString *) getSingleInfo:(NSString *)key {
    if ([key isEqualToString:@"deviceId"]) {
        return [SSDKPluginDeviceUtil getDeviceID];
    } else if ([key isEqualToString:@"os"]) {
        return [SSDKPluginDeviceUtil getOS];
    } else if ([key isEqualToString:@"osVersion"]) {
        return [SSDKPluginDeviceUtil getOSVersion];
    } else if ([key isEqualToString:@"appVersion"]) {
        return [SSDKPluginDeviceUtil getVersionName];
    } else if ([key isEqualToString:@"deviceType"]) {
        return [SSDKPluginDeviceUtil getDeviceType];
    } else if ([key isEqualToString:@"deviceModel"]) {
        return [SSDKPluginDeviceUtil getDeviceModel];
    } else if ([key isEqualToString:@"versionCode"]) {
        return [SSDKPluginDeviceUtil getVersionCode];
    } else if ([key isEqualToString:@"resolution"]) {
        return [SSDKPluginDeviceUtil getResolution];
    } else if ([key isEqualToString:@"CPUCores"] ||
               [key isEqualToString:@"curCPUFreq"] ||
               [key isEqualToString:@"maxCPUFreq"] ||
               [key isEqualToString:@"minCPUFreq"] ||
               [key isEqualToString:@"GPUModel"] ||
               [key isEqualToString:@"GPUShader"] ||
               [key isEqualToString:@"GPUVersion"]) {
        //暂时无法获取CPU,GPU的信息
        return @"";
    }
    return nil;
}

@end
